﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synthetic.Security.TripleDES
{
    public partial class Security : Form
    {
        private const string TITLE = "Secure File Encrypter";
        private const string ERROR_INVALID_FILE = "Invalid or no file selected!\nPlease select a valid file!";
        private const string SUCCESS_ENCRYPTED = "File encrypted!";
        private const string SUCCESS_DECRYPTED = "File decrypted!";
        private const string SUCCESS_KEYGEN = "Key and IV generated!";
        private const string ERROR_CATCH_FAIL = "It seems we've found an unknown bug!";
        private ErrorHandler e = new ErrorHandler();

        private enum DialogMode
        {
            SAVING,
            OPENING,
        }

        public Security()
        {
            InitializeComponent();
        }

        private void Security_Load(object sender, EventArgs e)
        {
            this.Text = TITLE;
        }

        private void SelectFile(TextBox Control, bool Encrypting, DialogMode Mode)
        {
            try
            {
                if (Mode == DialogMode.OPENING)
                {
                    OpenFileDialog dialog = new OpenFileDialog();
                    if (Encrypting == true)
                    {
                        dialog.Title = "Select file to encrypt..";
                    }
                    else
                    {
                        dialog.Title = "Select file to decrypt..";
                    }
                    dialog.RestoreDirectory = true;
                    dialog.Multiselect = false;
                    dialog.InitialDirectory = Application.StartupPath;
                    dialog.Filter = "All Files (*.*)|*.*";
                    DialogResult r = dialog.ShowDialog();
                    if (r == System.Windows.Forms.DialogResult.OK)
                    {
                        Control.Text = dialog.FileName;
                    }
                    else
                    {
                        DialogResult dr = MessageBox.Show(this, ERROR_INVALID_FILE, TITLE, MessageBoxButtons.RetryCancel);
                        if (dr == System.Windows.Forms.DialogResult.Retry)
                        {
                            SelectFile(Control, Encrypting, Mode);
                        }
                    }
                }
                else
                {
                    SaveFileDialog dialog = new SaveFileDialog();
                    if (Encrypting == true)
                    {
                        dialog.Title = "Save encrypted file..";
                    }
                    else
                    {
                        dialog.Title = "Save decrypted file..";
                    }
                    dialog.RestoreDirectory = true;
                    dialog.InitialDirectory = Application.StartupPath;
                    dialog.Filter = "All Files (*.*)|*.*";
                    DialogResult r = dialog.ShowDialog();
                    if (r == System.Windows.Forms.DialogResult.OK)
                    {
                        Control.Text = dialog.FileName;
                    }
                    else
                    {
                        DialogResult dr = MessageBox.Show(this, ERROR_INVALID_FILE, TITLE, MessageBoxButtons.RetryCancel);
                        if (dr == System.Windows.Forms.DialogResult.Retry)
                        {
                            SelectFile(Control, Encrypting, Mode);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                e.AddBugToQueue(ex.ToString());
            }
            
        }

        private void Operate(bool Encrypting)
        {
            if (Encrypting == true)
            {
                TripleDESService encrypter = new TripleDESService();
                encrypter.EncryptFile(encrypter_input.Text, encrypter_output.Text);
            }
            else
            {
                TripleDESService decrypter = new TripleDESService();
                decrypter.DecryptFile(decrypter_input.Text, decrypter_output.Text);
            }
        }

        private void encrypt_button_Click(object sender, EventArgs e)
        {
            Operate(true);
        }

        private void decrypt_button_Click(object sender, EventArgs e)
        {
            Operate(false);
        }

        private void decrypter_in_selector_Click(object sender, EventArgs e)
        {
            SelectFile(decrypter_input, false, DialogMode.OPENING);
        }

        private void decrypter_out_selector_Click(object sender, EventArgs e)
        {
            SelectFile(decrypter_output, false, DialogMode.SAVING);
        }

        private void encrypter_in_selector_Click(object sender, EventArgs e)
        {
            SelectFile(encrypter_input, true, DialogMode.OPENING);
        }

        private void encrypter_out_selector_Click(object sender, EventArgs e)
        {
            SelectFile(encrypter_output, true, DialogMode.SAVING);
        }

        private void Security_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (this.e.List() < 1)
                {
                    Application.Exit();
                }
                else if(this.e.List() == 1 | this.e.List() > 1)
                {
                    this.e.Send();
                }
            }
            catch
            {
                Application.Exit();
            }
        }
    }
}
